local GridLogic = require("bag.GridLogic")
local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local ItemDefine = require("bag.ItemDefine")
local ObjHuman = require("core.ObjHuman")
local BagLogic = require("bag.BagLogic")
local ShopHelp = require("shop.ShopHelp")

--返回仓库容量
function sendStoreCap(human)
	local msgRet = Msg.gc.GC_STORE_ADD_CAP
	msgRet.cap = human.db.storeLen
	Msg.send(msgRet, human.fd)	
	return true
end

--返回仓库列表
function sendStoreList(human, no_always_send)
	Grid.makeList(human,human.db.store, human.db.storeLen, Msg.gc.GC_STORE_LIST, not no_always_send and Msg.gc.GC_STORE_CLEAR)
	return true	
end

--发送仓库某格子物品的信息
function sendStoreItemChange(human, index)
	
	local msgRet = Msg.gc.GC_STORE_ITEM_CHANGE
	GridLogic.makeGrid(human.db.store[index] ,msgRet.itemData, index,human)
	Msg.send(msgRet, human.fd)
	
	return true		
end


--仓库整理
function sortStore(human)
    Grid.sort(human.db.store, human.db.storeLen)
	return sendStoreList(human)
end

--检查下标合法
function checkStoreIndex(human, index)
  return GridLogic.checkIndex(human.db.store, index, human.db.storeLen)
end

local function swap(human, srcIndex, tarIndex)
	if not checkStoreIndex(human,srcIndex) or not checkStoreIndex(human,tarIndex) then
		return false
	end
	local srcGrid = human.db.store[srcIndex]
  if srcGrid.id == 0 then
      return
  end

  local tarGrid = human.db.store[tarIndex]

  if srcGrid.id == tarGrid.id and ItemDefine.isItem(srcGrid.id) and ItemDefine.canAppend(srcGrid.id) and tarGrid.bind == 1 and srcGrid.bind == 0 then
    local cnt = math.min(srcGrid.cnt, ItemDefine.CAP_PER_GRID - tarGrid.cnt)
    tarGrid.cnt = tarGrid.cnt + cnt
    srcGrid.cnt = srcGrid.cnt - cnt
    if srcGrid.cnt <= 0 then
      human.db.store[srcIndex].id = 0
    end
  else
    local ret = GridLogic.swap(human.db.store,human.db.storeLen,srcIndex,tarIndex)
    if not ret then
      assert()
      return
    end
  end
	if srcGrid.cnt == 0 then
		srcGrid.id = 0
	end
		
	return true
end

--仓库物品移动
function swapStore(human, srcIndex, tarIndex)
	local ret = swap(human, srcIndex, tarIndex)
	if ret then
		sendStoreItemChange(human, srcIndex)
		sendStoreItemChange(human, tarIndex)
	end 
	return ret
end

--背包仓库间，物品移动
function swapStoreBag(human, posBag, posStore, isBag2Store)
	if not posStore or not posStore then
		return false
	end
	local ret = nil
	if isBag2Store == 1 then
		local grid = human.db.bag[posBag]
        if ItemDefine.getValue(grid.id, "canStore") == 1 then
            return false
        end
		local subType1 = grid and ItemDefine.getValue(grid.id,"subType1")
		local subType2 = grid and ItemDefine.getValue(grid.id,"subType2")
		if subType1 == 12 and subType2 == 10 then
			--Broadcast.sendBroadcast(human,Lang.MOUSE,Lang.BAG_ITEM_NOT_IN_STORE)
			return false
		end			
		ret = GridLogic.swap2(human.db.bag, posBag, human.db.store, posStore)
	else
		ret = GridLogic.swap2(human.db.store, posStore, human.db.bag, posBag)
	end
	return ret
end

--增加仓库容量
function addStoreCap(human, cnt)
	if human.db.storeLen >= ItemDefine.STORE_MAX_CAP then
		BagLogic.send_err(human, ItemDefine.BAG_ADD_STORE_MAX_CNT)
		return
	end
	if human.db.storeLen + cnt >= ItemDefine.STORE_MAX_CAP then
    return
	end
	if cnt < 1 then
	 return
	end
	local itemCnt = BagLogic.getItemCount(human, ItemDefine.ITEM_ID_ADD_CAP)
  if itemCnt >= cnt then
    BagLogic.delItemsById(human, ItemDefine.ITEM_ID_ADD_CAP, cnt, "add_cap")
  else  
    local totalNeedYuanbao = 0
    for i=1,cnt do
      totalNeedYuanbao = totalNeedYuanbao + ItemDefine.ADD_CAP_NEED_YUANBAO
    end
    if not ObjHuman.checkYuanbaoEnough(human, totalNeedYuanbao) then
      return ShopHelp.onNoRMB(human)
    end    
    if itemCnt > 0 then
      BagLogic.delItemsById(human, ItemDefine.ITEM_ID_ADD_CAP, itemCnt, "add_cap")
    end
    ObjHuman.decYuanbao(human,totalNeedYuanbao,"add_cap")    
  end

	human.db.storeLen = human.db.storeLen + cnt
	sendStoreCap(human)
end
--增加vip仓库格子
function addVipCap(human, cnt)
  if human.db.storeLen >= ItemDefine.STORE_MAX_CAP then
    BagLogic.send_err(human, ItemDefine.BAG_ADD_STORE_MAX_CNT)
    return
  end
  if human.db.storeLen + cnt >= ItemDefine.STORE_MAX_CAP then
    human.db.storeLen = ItemDefine.STORE_MAX_CAP
    sendStoreCap(human)
    return
  end
  if cnt < 1 then
    return
  end
  human.db.storeLen = human.db.storeLen + cnt
  sendStoreCap(human)
end

--判断是否还可以移入
function checkCanAddItem(human, id, bind, cnt, lifeTime)
	if cnt < 1 then
		return false
	end
	
	if not ItemDefine.isValidId(id) then
		return false
	end
	
	local store = human.db.store
	local capPerGrid = ItemDefine.CAP_PER_GRID
	
	for i = 1, store[0] do
		local grid = store[i]
		if not grid or grid.id == id and grid.bind == bind and grid.lifeTime == lifeTime and grid.cnt < capPerGrid and ItemDefine.canAppend(grid.id) then
			if not ItemDefine.canAppend(id) then
				cnt = cnt - 1
			else
				if grid then
					cnt = cnt - (capPerGrid - grid.cnt)
				else
					cnt = cnt - capPerGrid
				end
			end
			
			if cnt <= 0 then
				return true
			end
		end
	end
	
	return false
end


